﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using _PLATFORM.DATA;

namespace _PLATFORM.WEB.Controllers
{ 
    public class SupplierController : Controller
    {
        private eCommPlatformRepoEntities db = new eCommPlatformRepoEntities();

        //
        // GET: /Supplier/

        public ViewResult Index()
        {
            var suppliers = db.SUPPLIERs.Where(s => s.USER_ID == DATA.Custom_Classes.User.UserID);
            return View(suppliers.ToList());
        }

        //
        // GET: /Supplier/Details/5

        public ViewResult Details(decimal id)
        {
            SUPPLIER supplier = db.SUPPLIERs.Find(id);
            return View(supplier);
        }

        //
        // GET: /Supplier/Create

        public ActionResult Create()
        {
            ViewBag.USER_ID = new SelectList(db.FRAMEWORK_USER, "USER_ID", "FIRST_NAME");
            return View();
        } 

        //
        // POST: /Supplier/Create

        [HttpPost]
        public ActionResult Create(SUPPLIER supplier)
        {
            if (ModelState.IsValid)
            {
                supplier.USER_ID = DATA.Custom_Classes.User.UserID;
                db.SUPPLIERs.Add(supplier);

                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.USER_ID = new SelectList(db.FRAMEWORK_USER, "USER_ID", "FIRST_NAME", supplier.USER_ID);
            return View(supplier);
        }
        
        //
        // GET: /Supplier/Edit/5
 
        public ActionResult Edit(decimal id)
        {
            SUPPLIER supplier = db.SUPPLIERs.Find(id);
            ViewBag.USER_ID = new SelectList(db.FRAMEWORK_USER, "USER_ID", "FIRST_NAME", supplier.USER_ID);
            return View(supplier);
        }

        //
        // POST: /Supplier/Edit/5

        [HttpPost]
        public ActionResult Edit(SUPPLIER supplier)
        {
            if (ModelState.IsValid)
            {
                db.Entry(supplier).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.USER_ID = new SelectList(db.FRAMEWORK_USER, "USER_ID", "FIRST_NAME", supplier.USER_ID);
            return View(supplier);
        }

        //
        // GET: /Supplier/Delete/5
 
        public ActionResult Delete(decimal id)
        {
            SUPPLIER supplier = db.SUPPLIERs.Find(id);
            return View(supplier);
        }

        //
        // POST: /Supplier/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(decimal id)
        {            
            SUPPLIER supplier = db.SUPPLIERs.Find(id);
            db.SUPPLIERs.Remove(supplier);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}